package com.demo.config;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.type.JdbcType;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.Sequence;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.demo.json.JsonTypeHandler;

@Configuration
@EnableConfigurationProperties({MybatisPlusProperties.class})
public class BaseMybatisConfiguration {

    /**
     * 自定义格式需要生效
     * @return
     */
    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return configuration -> {
            configuration.getTypeHandlerRegistry().register(Map.class, JdbcType.VARCHAR, JsonTypeHandler.class);
            configuration.getTypeHandlerRegistry().register(List.class, JdbcType.VARCHAR, JsonTypeHandler.class);
            configuration.getTypeHandlerRegistry().register(Object.class, JdbcType.VARCHAR, JsonTypeHandler.class);
        };
    }
    /**
     * 分页插件
     * 
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

    /**
     * Mybatis Plus 注入器
     */
    @Bean("myMetaObjectHandler")
    @ConditionalOnMissingBean
    public MetaObjectHandler getMyMetaObjectHandler() {
        return new MyMetaObjectHandler(1, 1);
    }
    @Bean
    public Sequence sequence() {
        return new Sequence(1,1);
    }
}
